<template>
	<view>
		<view class="navbar">
			<view class="title">
				<text>首页</text>
			</view>
			<view class="search">
				<u-icon name="search" class="icon"></u-icon>
				<input type="text" placeholder="请输入关键字">
				<button @click="submit">搜索</button>
			</view>
		</view>
		<scroll-view scroll-y="true" class="main">
			<view class="content">
				<view class="swiper">
					<view class="wrap">
						<u-swiper :list="swiperlist" height="300" @click="to"></u-swiper>
					</view>
				</view>
				<view class="cate">
					<view class="cate-item" v-for="item in cateList">
						<image :src="$baseUrl + item.imgUrl" mode=""></image>
						<text class="name">{{item.serviceName}}</text>
					</view>
					<view class="cate-item">
						<u-icon name="list-dot" class="icon" size="80" color="#2979ff"></u-icon>
						<text class="name">更多服务</text>
					</view>
				</view>
				<view class="hot-pannel">
					<view class="title">热点新闻</view>
					<view class="hot">
						<navigator class="hot-item" v-for="item in hotList"
							:url="`/pages/newsdetail/newsdetail?id=${item.id}`">
							<image :src="$baseUrl + item.cover" mode=""></image>
							<text>{{item.title}}</text>
						</navigator>
					</view>
				</view>
				<view class="news">
					<u-sticky offset-top="90">
						<u-tabs :list="tabList" :is-scroll="true" :current="tabCurrent" @change="tabChange"></u-tabs>
					</u-sticky>
					<view class="news-list">
						<view class="news-item" v-for="item in selectList">
							<view class="left">
								<view class="top">
									<text class="name">
										{{item.title}}
									</text>
									<rich-text :nodes="item.content" class="desc"></rich-text>
								</view>
								<view class="bar">
									<text>{{item.commentNum}} 条评论</text>
									<text>{{item.publishDate}} 发布</text>
								</view>
							</view>
							<navigator :url="`/pages/newsdetail/newsdetail?id=${item.id}`" class="right">
								<image :src="$baseUrl + item.cover" mode=""></image>
							</navigator>
						</view>
						<u-divider>没有更多了</u-divider>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swiperlist: [],
				cateList: [],
				hotList: [],
				tabList: [],
				tabCurrent: 0,
				selectList: []
			}
		},
		onLoad() {
			this.getSwiper()
			this.getCate()
			this.getHot()
			this.getTab()
		},
		methods: {
			getSwiper() {
				this.$request({
					url: '/prod-api/api/rotation/list',
					data: {
						type: '2'
					}
				}).then(res => {
					this.swiperlist = res.rows.map(item => ({
						image: this.$baseUrl + item.advImg,
						id: item.targetId
					}))
					console.log(res)
				})
			},
			getCate() {
				this.$request({
					url: '/prod-api/api/service/list'
				}).then(res => {
					this.cateList = res.rows.splice(0, 9)
					console.log(this.cateList)
				})
			},
			to(index) {
				uni.navigateTo({
					url: `/pages/newsdetail/newsdetail?id=${this.swiperlist[index].id}`
				})
			},
			getHot() {
				this.$request({
					url: '/prod-api/press/press/list',
					data: {
						hot: 'Y'
					}
				}).then(res => {
					console.log(res)
					this.hotList = res.rows
				})
			},
			getTab() {
				this.$request({
					url: '/prod-api/press/category/list'
				}).then(res => {
					console.log(res)
					this.tabList = res.data.map(item => ({
						name: item.name,
						id: item.id
					}))
					this.getNews()
				})
			},
			tabChange(index) {
				this.tabCurrent = index
				this.getNews()
			},
			getNews() {
				this.$request({
					url: '/prod-api/press/press/list',
					data: {
						type: this.tabList[this.tabCurrent].id
					}
				}).then(res => {
					this.selectList = res.rows
					console.log(this.selectList)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		height: 100%;

		view {
			height: 100%;
			display: flex;
			flex-direction: column;

			.navbar {
				height: 180rpx;
				background-color: #fff;
				padding: 20rpx;

				.title {
					display: flex;
					flex-direction: row;
					justify-content: center;
					color: #dd6161;
					font-size: 45rpx;
					font-weight: bold;
					height: fit-content;
				}

				.search {
					flex-shrink: 0;
					position: relative;
					width: 95%;
					height: 64rpx;
					border-radius: 50rpx;
					border: 2rpx solid #dd6161;
					background-color: #f2f2f2;
					margin: 20rpx auto;
					display: flex;
					flex-direction: row;
					justify-content: start;
					align-items: center;

					.icon {
						margin-left: 10rpx;
					}

					input {
						margin-left: 10rpx;
					}

					button {
						width: 120rpx;
						height: 95%;
						display: flex;
						justify-content: center;
						align-items: center;
						background-color: #f4353a;
						color: white;
						position: absolute;
						right: 1rpx;
						font-size: 28rpx;
						border-radius: 50rpx;
					}
				}
			}

			.main {
				flex: 1;
				background-color: #f7f7f7;
				overflow: hidden;

				.content {
					height: fit-content;

					.swiper {
						background-color: #fff;
						padding: 20rpx;
					}

					.cate {
						display: flex;
						flex-direction: row;
						flex-wrap: wrap;
						justify-content: space-between;
						padding: 20rpx;

						.cate-item {
							width: 120rpx;
							display: flex;
							flex-direction: column;
							justify-content: center;
							align-items: center;
							height: fit-content;
							padding: 20rpx;

							image {
								width: 100rpx;
								height: 100rpx;
							}

							text {
								color: #666;
								width: 120rpx;
								font-size: 26rpx;
								text-align: center;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
								margin-top: 10rpx;
							}

							.icon {
								border-radius: 50%;
								border: 4rpx solid #2979ff;
								width: 100rpx;
								height: 100rpx;
								display: flex;
								justify-content: center;
								align-items: center;
							}
						}
					}

					.hot-pannel {
						display: flex;
						flex-direction: column;
						background-color: #fff;
						padding: 20rpx;

						.title {
							display: flex;
							align-items: flex-start;
							color: #dd6161;
							font-size: 45rpx;
							font-weight: 400;
							padding: 20rpx 0;
						}

						.hot {
							display: flex;
							flex-direction: row;
							justify-content: space-between;
							border-top: 2rpx solid #e4e7ed;
							padding: 20rpx 0;
							height: fit-content;

							.hot-item {
								display: flex;
								flex-direction: column;
								align-items: center;
								flex: 1;
								position: relative;
								height: fit-content;

								&:first-child::after {
									content: '';
									height: 60%;
									border-right: 2rpx solid #e4e7ed;
									position: absolute;
									right: 0;
									top: 20rpx;
								}

								image {
									width: 300rpx;
									height: 250rpx;
									border-radius: 20rpx;
								}

								text {
									width: 300rpx;
									display: -webkit-box;
									-webkit-box-orient: vertical;
									-webkit-line-clamp: 2;
									overflow: hidden;
									margin-top: 20rpx;
									color: #666;
								}
							}
						}
					}

					.news {
						margin-top: 20rpx;
						background-color: #fff;

						.news-list {
							padding: 20rpx;

							.news-item {
								display: flex;
								flex-direction: row;
								justify-content: space-between;
								padding: 20rpx 0;
								border-top: 2rpx solid #e4e7ed;

								.left {
									display: flex;
									flex-direction: column;

									.top {
										width: 450rpx;
										height: 168rpx;

										text {
											display: -webkit-box;
											-webkit-box-orient: vertical;
											-webkit-line-clamp: 2;
											overflow: hidden;
											font-size: 32rpx;
											font-weight: bold;
											line-height: 1.5;
										}

										.desc {
											display: -webkit-box;
											-webkit-box-orient: vertical;
											-webkit-line-clamp: 2;
											overflow: hidden;
											font-size: 26rpx;
											line-height: 1.5;
										}
									}

									.bar {
										display: flex;
										flex-direction: row;
										justify-content: start;

										text {
											margin-left: 20rpx;
											font-size: 22rpx;
											color: #909399;

											&:first-child {
												margin-left: 0;
											}
										}
									}

								}

								.right {
									display: flex;
									justify-content: center;
									align-items: center;

									image {
										width: 250rpx;
										height: 200rpx;
									}
								}
							}
						}
					}
				}
			}
		}
	}
</style>